package com.yanqu.road.server.cmd.union.cross.huaben;

import com.yanqu.road.entity.player.CrossUserBaseInfo;
import com.yanqu.road.entity.union.huaben.UnionHuaBenAttackDamageData;
import com.yanqu.road.entity.union.huaben.UnionHuaBenUserAttack;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.union.cross.UnionHuaBenProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionReqContext;
import com.yanqu.road.server.manager.union.huaben.CrossUnionHuaBenConfigMgr;
import com.yanqu.road.server.manager.union.huaben.CrossUnionHuaBenMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.C_CROSS_UNION_HUA_BEN_DAMAGE_RECORD, desc = "伤害记录")
public class CrossUnionHuaBenDamageRecordCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        UnionHuaBenProto.UnionHuaBenDamageRecordReqMsg reqMsg = UnionHuaBenProto.UnionHuaBenDamageRecordReqMsg.parseFrom(packet.getBytes());
        int level = reqMsg.getLevel();

        UnionHuaBenProto.UnionHuaBenDamageRecordRespMsg.Builder respMsg = UnionHuaBenProto.UnionHuaBenDamageRecordRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setLevel(level);

        int weekId = CrossUnionHuaBenConfigMgr.getThisWeekId();

        CrossUnionReqContext context = CrossUnionReqContext.initContext(serverId, packet.getPlayerId()).loadUnionMember().loadUnionInfo();
        if (context.getRet() != 0) {
            respMsg.setRet(context.getRet());
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_CROSS_UNION_HUA_BEN_DAMAGE_RECORD, respMsg));
        }

        UnionHuaBenAttackDamageData damageData = CrossUnionHuaBenMgr.getUnionHuaBenAttackDamageData(weekId, context.getUnionInfo().getServerId(), context.getUnionInfo().getUnionUid(), level);
        if (damageData != null) {
            Map<Long, UnionHuaBenUserAttack> userDamageMap = new HashMap<>();

            for (Map.Entry<Integer, List<UnionHuaBenUserAttack>> entry : damageData.getDamageMap().entrySet()) {
                for (int i = 0; i < entry.getValue().size(); i++) {
                    UnionHuaBenUserAttack tempData = entry.getValue().get(i);
                    UnionHuaBenUserAttack attack = userDamageMap.get(tempData.getUserId());
                    if (attack == null) {
                        attack = new UnionHuaBenUserAttack();
                        attack.setUserId(tempData.getUserId());
                        attack.setServerId(tempData.getServerId());
                        attack.setAttackTime(tempData.getAttackTime());
                        userDamageMap.put(attack.getUserId(), attack);
                    }
                    if (tempData.getAttackTime() < attack.getAttackTime()) {
                        attack.setAttackTime(tempData.getAttackTime());
                    }
                    attack.setDamage(new BigInteger(attack.getDamage()).add(new BigInteger(tempData.getDamage())).toString());
                }
            }

            for (Map.Entry<Long, UnionHuaBenUserAttack> entry : userDamageMap.entrySet()) {
                UnionHuaBenUserAttack attack = entry.getValue();
                UnionHuaBenProto.UnionHuaBenDamageRecordTemp.Builder recordBuild = UnionHuaBenProto.UnionHuaBenDamageRecordTemp.newBuilder();
                recordBuild.setDamage(attack.getDamage());
                recordBuild.setUserId(attack.getUserId());
                recordBuild.setServerId(attack.getServerId());
                CrossUserBaseInfo baseInfo = CrossUserMgr.getCrossUserBaseInfo(attack.getUserId());
                if (baseInfo != null) {
                    recordBuild.setNickName(baseInfo.getUserBaseInfo().getNickName());
                    recordBuild.setPlayerBase(PlayerBasePb.parsePlayerBaseTempMsg(baseInfo.getUserBaseInfo()));
                }
                recordBuild.setTime(attack.getAttackTime());
                respMsg.addDamage(recordBuild);
            }
        }

        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_CROSS_UNION_HUA_BEN_DAMAGE_RECORD, respMsg));
    }

    @Override
    public void sendData() {

    }
}
